

import Database from "@tauri-apps/plugin-sql";

import { getG01DataBase, homePath } from '../../utils';

import { MetaDataObjectProperty, MetaDataTrait } from '../types';


const TABLE_NAME = '_g01_meta_traits';


export const metaDataGetTraits = async (domainIdentifier: string, page: number, pageSize: number): Promise<{ total: number, data: Array<MetaDataTrait> }> => {

    const db = await getG01DataBase();

    let offset = (page - 1) * pageSize;

    let conditions = `  where domain_identifier = $1 `;
    let args = [domainIdentifier];


    let count = (await db.select(`select count(1) cnt from ${TABLE_NAME} ${conditions}`, args))[0].cnt;

    //
    let items: Array<any> = await db.select(`select * from ${TABLE_NAME} ${conditions} order by id desc limit ${offset},${pageSize} `, args);
    if (items) {
        return {
            total: count,
            data: items.map((item, index) => {
                return {
                    id: item.id,
                    domainIdentifier: item.domain_identifier,
                    name: item.name,
                    identifier: item.identifier,
                    traitType: item.trait_type,
                    path: item.path,
                    description: item.description,
                }
            }),
        };
    }
    return {
        total: 0,
        data: []
    };
}


export const metaDataGetTraitsByType = async (domainIdentifier: string, traitType: string): Promise<Array<MetaDataTrait>> => {

    const db = await getG01DataBase();
    //
    let items: Array<any> = await db.select(`select * from  ${TABLE_NAME} where (domain_identifier = '' or domain_identifier = $1) and trait_type = $2`, [domainIdentifier, traitType]);
    if (items) {
        //
        return items.map((item, index) => {
            return {
                id: item.id,
                domainIdentifier: item.domain_identifier,
                name: item.name,
                identifier: item.identifier,
                traitType: item.trait_type,
                path: item.path,
                description: item.description,
            }
        });
    }
    return [];
}

export const metaDataSaveTrait = async (obj: MetaDataTrait): Promise<number> => {

    // 
    const db = await getG01DataBase();

    let items: Array<any> = await db.select(`select * from  ${TABLE_NAME} where domain_identifier = $1 and identifier = $2 `, [obj.domainIdentifier, obj.identifier]);

    if (items && items.length > 0) {
        //更新
        return await metaDataUpdateTrait(obj);
    } else {
        //添加
        //
        await db.execute(`insert into ${TABLE_NAME}(domain_identifier,identifier,name,icon,trait_type,path,description)Values($1,$2,$3,$4,$5,$6,$7)`,
            [obj.domainIdentifier, obj.identifier, obj.name, obj.icon, obj.traitType, obj.path, obj.description]);

        let res: Array<any> = await db.select('SELECT last_insert_rowid() nid');
        console.log(res);
        if (res && res.length > 0) {
            return res[0].nid;
        }
    }

    return 0;
}

export const metaDataUpdateTrait = async (obj: MetaDataTrait): Promise<number> => {

    // 
    const db = await getG01DataBase();
    //
    await db.execute(`update ${TABLE_NAME} set identifier = $1, name = $2, icon = $3, trait_type = $4, path = $5, description = $6 where id = $7`,
        [obj.identifier, obj.name, obj.icon, obj.traitType, obj.path, obj.description, obj.id]);
    return 0;
}


export const metaDataRemoveTraitById = async (id: number): Promise<void> => {
    // 
    const db = await getG01DataBase();
    //
    if (id > 0) {
        await db.execute(`delete from ${TABLE_NAME}  where id = $1`, [id]);
    }

}


export default {};